//
//  UIBarButtonItem+Extension.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/12.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit

/*便利构造函数可在对已有的类进行扩展，简化对象的创建
   只能调用self.init(),不能被重写或重写
 */
extension UIBarButtonItem {
    
    convenience init(imageName: String? = nil,  title: String = "",target: Any?,action: Selector?  ) {
        
        let btn = UIButton()
        if  let img = imageName {
            
            btn.setImage(UIImage(named: img), for: .normal)
            btn.setImage(UIImage(named: img + "_highlighted"), for: .highlighted)
        }
        //字体
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.sizeToFit()
        //监听点击事件
        if let ac = action {
            
            btn.addTarget(target, action: ac, for: .touchUpInside)
        }
        btn.sizeToFit()
        //在便利构造函数中不提示
        self.init()
        self.customView = btn
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
